package com.vdg.lockphotos.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.vdg.lockphotos.ListPhotosActivity;
import com.vdg.lockphotos.R;
import com.vdg.lockphotos.app.ApplicationManagement;
import com.vdg.lockphotos.data.Album;
import com.vdg.lockphotos.gridphotos.ListAlbumsAdapter;
import com.vdg.lockphotos.notify.OnLoadAlbumsListener;

public class ListAlbumsFragment extends Fragment {
	private ListView mLivAlbums;
	private ArrayList<Album> mListAlbums = new ArrayList<Album>();
	private ListAlbumsAdapter mListAlbumsAdapter;
	private Handler mHandler = new Handler();
	private LinearLayout mLnAds;
	private FrameLayout mFrmDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.albums_layout, container,
				false);
		init(rootView);
		ApplicationManagement.getInstance(getActivity()).loadAllAlbums(
				new OnLoadAlbumsListener() {

					@Override
					public void onStartLoad() {
						// TODO Auto-generated method stub
						mFrmDialog.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadSucessed(ArrayList<Album> listPhoto) {
						// TODO Auto-generated method stub
						mListAlbums.clear();
						mListAlbums.addAll(listPhoto);
						mListAlbumsAdapter.notifyDataSetChanged();
						mFrmDialog.setVisibility(View.GONE);
					}

					@Override
					public void onLoadFailed() {
						// TODO Auto-generated method stub
						mFrmDialog.setVisibility(View.GONE);
					}
				});
		// loadImage();
		// TODO Auto-generated method stub
		return rootView;
	}

	private void init(View rootView) {
		mLivAlbums = (ListView) rootView.findViewById(R.id.liv_albums);
		mListAlbumsAdapter = new ListAlbumsAdapter(getActivity(), 0,
				mListAlbums);
		mLivAlbums.setAdapter(mListAlbumsAdapter);
		mLivAlbums.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ListPhotosActivity.class);
				intent.putExtra(ListPhotosActivity.ALBUM_URL_KEY, mListAlbums
						.get(position).getName());
				getActivity().startActivity(intent);
			}
		});
		mLnAds = (LinearLayout) rootView.findViewById(R.id.ln_ads);
		AdSize testSize = AdSize.createAdSize(AdSize.SMART_BANNER,
				getActivity());
		AdView ad = (AdView) rootView.findViewById(R.id.adView);
		ad.loadAd(new AdRequest().addTestDevice(getResources().getString(
				R.string.test_devices_id)));
		mFrmDialog = (FrameLayout) rootView.findViewById(R.id.frm_dialog);
	}

}
